home *** CD-ROM | disk | FTP | other *** search
/ Freelog 22 / freelog 22.iso / Prog / Djgpp / GPC2952B.ZIP / lib / gcc-lib / djgpp / 2.952 / units / regexc.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-02-08  |  4.5 KB  |  147 lines

  1. /*
  2. Support routines for regex.pas
  3.  
  4. Copyright (C) 1998-2001 Free Software Foundation, Inc.
  5.  
  6. Author: Frank Heckenbach <frank@pascal.gnu.de>
  7.  
  8. This file is part of GNU Pascal.
  9.  
  10. GNU Pascal is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License as published by
  12. the Free Software Foundation; either version 2, or (at your option)
  13. any later version.
  14.  
  15. GNU Pascal is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License for more details.
  19.  
  20. You should have received a copy of the GNU General Public License
  21. along with GNU Pascal; see the file COPYING. If not, write to the
  22. Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  23. 02111-1307, USA.
  24.  
  25. As a special exception, if you link this file with files compiled
  26. with a GNU compiler to produce an executable, this does not cause
  27. the resulting executable to be covered by the GNU General Public
  28. License. This exception does not however invalidate any other
  29. reasons why the executable file might be covered by the GNU General
  30. Public License.
  31. */
  32.  
  33. #include <stdlib.h>
  34. #include <rxposix.h>
  35.  
  36. typedef unsigned char Boolean;
  37.  
  38. typedef struct
  39. {
  40.   regex_t    *RegEx;
  41.   regmatch_t *RegMatch;
  42.   char       *ErrorInternal;
  43.   int         From, Length, SubExpressions;
  44.   void       *Error;
  45. } RegExType;
  46.  
  47. void _p_new_regex (RegExType *r, const char *Expression, int ExpressionLength,
  48.                    Boolean ExtendedRegEx, Boolean CaseInsensitive, Boolean NewLines);
  49. void _p_new_regex (RegExType *r, const char *Expression, int ExpressionLength,
  50.                    Boolean ExtendedRegEx, Boolean CaseInsensitive, Boolean NewLines)
  51. {
  52.   int result;
  53.   r -> ErrorInternal = NULL;
  54.   r -> Error = NULL;
  55.   r -> From = 1;
  56.   r -> Length = 0;
  57.   r -> SubExpressions = 0;
  58.   r -> RegMatch = 0;
  59.   r -> RegEx = (regex_t *) malloc (sizeof (regex_t));
  60.   result = regncomp (r -> RegEx, Expression, ExpressionLength,
  61.             (ExtendedRegEx    ? REG_EXTENDED : 0) |
  62.             (CaseInsensitive  ? REG_ICASE    : 0) |
  63.             (NewLines         ? REG_NEWLINE  : 0));
  64.   if (result)
  65.     {
  66.       if (r -> ErrorInternal)
  67.         {
  68.           free (r -> ErrorInternal);
  69.           r -> ErrorInternal = 0;
  70.         }
  71.       if (result)
  72.         {
  73.           size_t size = regerror (result, r -> RegEx, 0, 0);
  74.           r -> ErrorInternal = (char *) malloc (size);
  75.           regerror (result, r -> RegEx, r -> ErrorInternal, size);
  76.         }
  77.       free (r -> RegEx);
  78.       r -> RegEx = 0;
  79.     }
  80.   else
  81.     {
  82.       r -> SubExpressions = r -> RegEx -> re_nsub - 1;
  83.       r -> RegMatch = (regmatch_t *) malloc ((r -> SubExpressions + 1) * sizeof (regmatch_t));
  84.     }
  85. }
  86.  
  87. void _p_dispose_regex (RegExType *r);
  88. void _p_dispose_regex (RegExType *r)
  89. {
  90.   if (r -> RegEx)
  91.     {
  92.       regfree (r -> RegEx);
  93.       free (r -> RegEx);
  94.       r -> RegEx = 0;
  95.     }
  96.   if (r -> ErrorInternal)
  97.     {
  98.       free (r -> ErrorInternal);
  99.       r -> ErrorInternal = 0;
  100.     }
  101.   if (r -> Error)
  102.     {
  103.       free (r -> Error);
  104.       r -> Error = 0;
  105.     }
  106.   if (r -> RegMatch)
  107.     {
  108.       free (r -> RegMatch);
  109.       r -> RegMatch = 0;
  110.       r -> SubExpressions = -1;
  111.     }
  112. }
  113.  
  114. Boolean _p_match_regex_from (RegExType *r, const char *aString, int StrLength,
  115.                              Boolean NotBeginningOfLine, Boolean NotEndOfLine, int From);
  116. Boolean _p_match_regex_from (RegExType *r, const char *aString, int StrLength,
  117.                              Boolean NotBeginningOfLine, Boolean NotEndOfLine, int From)
  118. {
  119.   int i = r -> Length = StrLength - From + 1;
  120.   r -> From = From;
  121.   return !regnexec (r -> RegEx, (i >= 0) ? aString + From - 1 : aString, (i >= 0) ? i : 0, r -> SubExpressions + 1,
  122.                     &r -> RegMatch, (NotBeginningOfLine ? REG_NOTBOL : 0) |
  123.                     (NotEndOfLine ? REG_NOTEOL : 0));
  124. }
  125.  
  126. void _p_getmatch_regex (RegExType *r, int n, int *MatchPosition, int *MatchLength);
  127. void _p_getmatch_regex (RegExType *r, int n, int *MatchPosition, int *MatchLength)
  128. {
  129.   int MPosition, MLength;
  130.   if (n < 0 || n > r -> SubExpressions)
  131.     MPosition = MLength = 0;
  132.   else
  133.     {
  134.       MPosition = r -> RegMatch [n].rm_so;
  135.       MLength   = r -> RegMatch [n].rm_eo;
  136.       if (MPosition >= 0 && MLength >= 0 && MLength <= r -> Length)
  137.         {
  138.           MLength -= MPosition;
  139.           MPosition += r -> From;
  140.         }
  141.       else
  142.         MPosition = MLength = 0;
  143.     }
  144.   if (MatchPosition) *MatchPosition = MPosition;
  145.   if (MatchLength) *MatchLength = MLength;
  146. }
  147.